import lodash from "lodash"
/**阴影算法 */
export function queryWidgetByShadowAlgorithm(widgetList, direction, widgetItem) {
  const currentWidget = document.getElementById(widgetItem.uid)
  var currentRect = getBoundingClientRect(currentWidget);
  var resultWidget = null;
  var resultItem = null;
  var dis = Infinity,
    tempDis;
  for (var i = 0; i < widgetList.length; i++) {
    let _widgetId = widgetList[i].uid
    if (widgetItem.scrollId && widgetList[i].scrollId && (widgetItem.scrollId !== widgetList[i].scrollId)) {
      _widgetId = widgetList[i].scrollId
    }
    var tempWidget = document.getElementById(_widgetId);
    if (tempWidget === currentWidget) {
      continue;
    }
    var tempWidgetRect = getBoundingClientRect(tempWidget);
    if (tempWidgetRect.height == 0 || tempWidgetRect.width == 0) continue;
    if (
      direction === "LEFT" &&
      tempWidgetRect.right < currentRect.centerX &&
      tempWidgetRect.left < currentRect.left &&
      tempWidgetRect.top < currentRect.bottom &&
      tempWidgetRect.bottom > currentRect.top
    ) {
      tempDis = currentRect.right - tempWidgetRect.right;
      if (
        tempDis < dis ||
        (tempDis === dis &&
          Math.abs(currentRect.centerY - tempWidgetRect.centerY) <
          Math.abs(
            currentRect.centerY - getBoundingClientRect(resultWidget).centerY
          ))
      ) {
        resultWidget = tempWidget;
        resultItem = widgetList[i]
        dis = tempDis;
      }
    } else if (
      direction === "RIGHT" &&
      tempWidgetRect.left > currentRect.centerX &&
      tempWidgetRect.right > currentRect.right &&
      tempWidgetRect.top < currentRect.bottom &&
      tempWidgetRect.bottom > currentRect.top
    ) {
      tempDis = tempWidgetRect.left - currentRect.left;
      if (
        tempDis < dis ||
        (tempDis === dis &&
          Math.abs(currentRect.centerY - tempWidgetRect.centerY) <
          Math.abs(
            currentRect.centerY - getBoundingClientRect(resultWidget).centerY
          ))
      ) {
        resultWidget = tempWidget;
        resultItem = widgetList[i]
        dis = tempDis;
      }
    } else if (
      direction === "UP" &&
      tempWidgetRect.bottom < currentRect.centerY &&
      tempWidgetRect.top < currentRect.top &&
      tempWidgetRect.left < currentRect.right &&
      tempWidgetRect.right > currentRect.left
    ) {
      tempDis = currentRect.bottom - tempWidgetRect.bottom;
      if (
        tempDis < dis ||
        (tempDis === dis &&
          Math.abs(currentRect.centerX - tempWidgetRect.centerX) <
          Math.abs(
            currentRect.centerX - getBoundingClientRect(resultWidget).centerX
          ))
      ) {
        resultWidget = tempWidget;
        resultItem = widgetList[i]
        dis = tempDis;
      }
    } else if (
      direction === "DOWN" &&
      tempWidgetRect.top > currentRect.centerY &&
      tempWidgetRect.bottom > currentRect.bottom &&
      tempWidgetRect.left < currentRect.right &&
      tempWidgetRect.right > currentRect.left
    ) {
      tempDis = tempWidgetRect.top - currentRect.top;
      if (
        tempDis < dis ||
        (tempDis === dis &&
          Math.abs(currentRect.centerX - tempWidgetRect.centerX) <
          Math.abs(
            currentRect.centerX - getBoundingClientRect(resultWidget).centerX
          ))
      ) {
        resultWidget = tempWidget;
        resultItem = widgetList[i]
        dis = tempDis;
      }
    }
  }
  return resultItem
}


/**区域算法 */
export function queryWidgetByAreaAlgorithm(widgetList, direction, widgetItem) {
  const currentWidget = document.getElementById(widgetItem.uid)
  var currentRect = getBoundingClientRect(currentWidget);
  var resultWidget = null;
  var resultItem = null;
  var dis = Infinity,
    tempDis;
  for (var i = 0; i < widgetList.length; i++) {
    let _widgetId = widgetList[i].uid
    if (widgetItem.scrollId && widgetList[i].scrollId && (widgetItem.scrollId !== widgetList[i].scrollId)) {
      _widgetId = widgetList[i].scrollId
    }
    var tempWidget = document.getElementById(_widgetId);
    if (tempWidget === currentWidget) {
      continue;
    }
    var tempWidgetRect = getBoundingClientRect(tempWidget);
    if (tempWidgetRect.height == 0 || tempWidgetRect.width == 0) continue;

    if (direction === "LEFT" && tempWidgetRect.right <= currentRect.left) {
      tempDis = Infinity;
      if (tempWidgetRect.bottom < currentRect.top) {
        tempDis = Math.sqrt(
          Math.pow(currentRect.left - tempWidgetRect.right, 2) +
          Math.pow(currentRect.top - tempWidgetRect.bottom, 2)
        );
      } else if (tempWidgetRect.top > currentRect.bottom) {
        tempDis = Math.sqrt(
          Math.pow(currentRect.left - tempWidgetRect.right, 2) +
          Math.pow(tempWidgetRect.top - currentRect.bottom, 2)
        );
      } else {
        tempDis = currentRect.left - tempWidgetRect.right;
      }
      if (tempDis < dis) {
        resultWidget = tempWidget;
        resultItem = widgetList[i]
        dis = tempDis;
      }
    } else if (
      direction === "RIGHT" &&
      tempWidgetRect.left >= currentRect.right
    ) {
      tempDis = Infinity;
      if (tempWidgetRect.bottom < currentRect.top) {
        tempDis = Math.sqrt(
          Math.pow(tempWidgetRect.left - currentRect.right, 2) +
          Math.pow(currentRect.top - tempWidgetRect.bottom, 2)
        );
      } else if (tempWidgetRect.top > currentRect.bottom) {
        tempDis = Math.sqrt(
          Math.pow(tempWidgetRect.left - currentRect.right, 2) +
          Math.pow(tempWidgetRect.top - currentRect.bottom, 2)
        );
      } else {
        tempDis = tempWidgetRect.left - currentRect.right;
      }
      if (tempDis < dis) {
        resultWidget = tempWidget;
        resultItem = widgetList[i]
        dis = tempDis;
      }
    } else if (direction === "UP" && tempWidgetRect.bottom <= currentRect.top) {
      tempDis = Infinity;
      if (tempWidgetRect.right < currentRect.left) {
        tempDis = Math.sqrt(
          Math.pow(currentRect.top - tempWidgetRect.bottom, 2) +
          Math.pow(currentRect.left - tempWidgetRect.right, 2)
        );
      } else if (tempWidgetRect.left > currentRect.right) {
        tempDis = Math.sqrt(
          Math.pow(currentRect.top - tempWidgetRect.bottom, 2) +
          Math.pow(tempWidgetRect.left - currentRect.right, 2)
        );
      } else {
        tempDis = currentRect.top - tempWidgetRect.bottom;
      }
      if (tempDis < dis) {
        resultWidget = tempWidget;
        resultItem = widgetList[i]
        dis = tempDis;
      }
    } else if (direction === "DOWN" && tempWidgetRect.top >= currentRect.bottom) {
      tempDis = Infinity;
      if (tempWidgetRect.right < currentRect.left) {
        tempDis = Math.sqrt(
          Math.pow(tempWidgetRect.top - currentRect.bottom, 2) +
          Math.pow(currentRect.left - tempWidgetRect.right, 2)
        );
      } else if (tempWidgetRect.left > currentRect.right) {
        tempDis = Math.sqrt(
          Math.pow(tempWidgetRect.top - currentRect.bottom, 2) +
          Math.pow(tempWidgetRect.left - currentRect.right, 2)
        );
      } else {
        tempDis = tempWidgetRect.top - currentRect.bottom;
      }
      if (tempDis < dis) {
        resultWidget = tempWidget;
        resultItem = widgetList[i]
        dis = tempDis;
      }
    }
  }
  return resultItem
}
export function queryScroll(widgetList, direction, widgetItem) {
  if (!widgetItem.scrollId) return null
  const _list = lodash.filter(widgetList, (item) => {
    return item.scrollId === widgetItem.scrollId
  })
  let resultWidget = null
  const _idx = lodash.findIndex(_list, (item) => item.uid === widgetItem.uid)
  if (widgetItem.scrollOrientation === "y" && (direction === "UP" || direction === "DOWN")) {
    const _item = _list[direction === "UP" ? _idx - 1 : _idx + 1]
    resultWidget = _item ? _item.uid : null
  }
  if (widgetItem.scrollOrientation === "x" && (direction === "LEFT" || direction === "RIGHT")) {
    const _item = _list[direction === "UP" ? _idx - 1 : _idx + 1]
    resultWidget = _item ? _item.uid : null
  }
  return resultWidget
}



export function getBoundingClientRect(el) {
  if (!el) {
    return { left: 0, top: 0, width: 0, height: 0, right: 0, bottom: 0, x: 0, y: 0, centerX: 0, centerY: 0, }
  }
  const rect = el.getBoundingClientRect() || {};
  const scrollLeft = window.pageXOffset || document.documentElement.scrollLeft;
  const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
  const clientLeft = document.documentElement.clientLeft || 0;
  const clientTop = document.documentElement.clientTop || 0;
  return {
    left: rect.left + scrollLeft - clientLeft,
    top: rect.top + scrollTop - clientTop,
    width: rect.width,
    height: rect.height,
    right: rect.right + scrollLeft - clientLeft,
    bottom: rect.bottom + scrollTop - clientTop,
    x: rect.x || rect.left,
    y: rect.y || rect.top,
    centerX: rect.width / 2 + rect.left,
    centerY: rect.height / 2 + rect.top,
    toJSON: rect.toJSON || (() => { }),
    ...(rect)
  }
}